<template>
  <el-row :gutter="10">
    <!--左侧部门选择-->
    <el-col :span="8">
      <div class="head-container">
        <el-input
          v-model="deptName"
          clearable
          size="small"
          placeholder="输入部门名称搜索"
          prefix-icon="el-icon-search"
          class="filter-item"
          @input="getDeptDatas"
        />
      </div>
      <el-tree
        :data="deptDatas"
        :load="getDeptDatas"
        :props="defaultProps"
        :expand-on-click-node="false"
        lazy
        @node-click="handleNodeClick"
      />
    </el-col>
    <el-col :span="16">
      <div class="head-container">
        <div>
          <!-- 搜索 -->
          <el-input
            v-model="query.blurry"
            clearable
            size="small"
            placeholder="请输入用户名"
            style="width: 200px;"
            class="filter-item"
            @keyup.enter.native="crud.toQuery"
          />
          <rrOperation />
        </div>
      </div>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        highlight-current-row
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
        @current-change="handleCurrentChange"
      >
        <el-table-column :show-overflow-tooltip="true" prop="username" label="用户名" />
        <el-table-column :show-overflow-tooltip="true" prop="nickName" label="昵称" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </el-col>
  </el-row>
</template>

<script>
import CRUD, { presenter, header } from '@crud/crud'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import { getDepts } from '@/api/system/dept'
import crudUser from '@/api/system/user'
export default {
  name: 'UserSelect',
  components: { pagination, rrOperation },
  cruds() {
    return [
      CRUD({
        title: '用户查询', url: 'rest/api/user', query: { organization: { id: null }},
        crudMethod: { ...crudUser },
        optShow: {
          add: true,
          edit: true,
          del: true,
          reset: false
        },
        queryOnPresenterCreated: false
      })
    ]
  },
  mixins: [
    presenter(),
    header()],
  data() {
    return {
      deptName: '',
      deptId: null,
      depts: [],
      deptDatas: [],
      defaultProps: { children: 'children', label: 'name', isLeaf: 'leaf' },
      permission: {
        add: ['admin', 'user:add'],
        edit: ['admin', 'user:edit'],
        del: ['admin', 'user:del']
      }
    }
  },
  methods: {
    // 获取左侧部门数据
    getDeptDatas(node, resolve) {
      const sort = 'id,desc'
      const params = { sort: sort }
      if (typeof node !== 'object') {
        if (node) {
          params['blurry'] = node
        }
      } else if (node.level !== 0) {
        params['pid'] = node.data.id
      }
      setTimeout(() => {
        getDepts(params).then(res => {
          if (resolve) {
            resolve(res.content)
          } else {
            this.deptDatas = res.content
          }
        })
      }, 100)
    },
    getDepts() {
      getDepts({ enabled: true }).then(res => {
        this.depts = res.content.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    // 切换部门
    handleNodeClick(data) {
      if (data.pid === 0) {
        this.query.deptId = null
      } else {
        this.query.deptId = data.id
      }
      this.crud.toQuery()
    },
    // 获取选中用户
    handleCurrentChange(data) {
      if (data != null) {
        this.$emit('selected', data)
      }
    },
    reset() {
      this.deptName = ''
    }
  }
}
</script>

<style scoped>

</style>
